<template>
  <div>
    <el-row>
      <!--    main的div-->
      <el-col :span="12"><div id="main1" style="width: 500px; height: 400px"></div></el-col>
      <el-col :span="12"><div id="main2" style="width: 500px; height: 400px;"></div></el-col>
    </el-row>
    <el-row>
      <el-col :span="12"><div id="main3" style="width: 500px; height: 400px;"></div></el-col>
    </el-row>
  </div>
</template>

<script>

import * as echarts from 'echarts';

export default {
  name: "Home",
  data() {
    return {

    }
  },
  //页面元素渲染之后再触发，比如渲染main这个div后再触发
  mounted() {
    //这时候才能获取到
    var option1 = {
      xAxis: {
        type: 'category',
        data: []
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: [],
          type: 'line'
        },
      ]
    };
    var chartDom1 = document.getElementById('main1');
    var myChart1 = echarts.init(chartDom1);

    this.request.get("/echarts/example").then(res => {
      //接收后端数据后
      option1.xAxis.data = res.data.x;
      option1.series[0].data = res.data.y;
      //放入数据
      option1 && myChart1.setOption(option1);
    });


    //柱状图 和 柱状图一样
    var option2 = {
      title: {
        text: '各季度会员统计',
        subtext: '会员趋势图',
        left: 'center'
      },
      xAxis: {
        type: 'category',
        data: ['第一季度', '第二季度', '第三季度', '第四季度']
      },
      yAxis: {
        type: 'value'
      },
      series: [
        {
          data: [],
          type: 'bar'
        },
      ]
    };
    var chartDom2 = document.getElementById('main2');
    var myChart2 = echarts.init(chartDom2);
    this.request.get("/echarts/members").then(res => {
      //接收后端数据后
      option2.series[0].data = res.data;
      //放入数据
      option2 && myChart2.setOption(option2);
    });

    //饼图
    var option3 = {
      title: {
        text: '各季度会员统计',
        subtext: '比例图',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          type: 'pie',
          radius: '80%',
          label: {  //饼图上文本标签
            normal: {
              show: true,
              position: 'inner',  //标签位置
              textStyle: {
                fontWeight: 300,  //字体大小
                fontSize: 16,
                color: "#fff"
              },
              formatter: '{d}%'
            }
          },
          data: [], //参数形式 ==> { value: 1048, name: 'Search Engine' },
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    };
    var chartDom3 = document.getElementById('main3');
    var myChart3 = echarts.init(chartDom3);
    this.request.get("/echarts/members").then(res => {
      //接收后端数据后
      option3.series[0].data = [
        {name: "第一季度", value: res.data[0]},
        {name: "第二季度", value: res.data[1]},
        {name: "第三季度", value: res.data[2]},
        {name: "第四季度", value: res.data[3]},
      ];
      //放入数据
      option3 && myChart3.setOption(option3);
    });


  }
}
</script>

<style scoped>

</style>